// 实现这个项目的构建任务
const sass = require('sass')
const loadGruntTasks = require('load-grunt-tasks')
const pkg = {
    menus: [
        {
            name: 'Home',
            icon: 'aperture',
            link: 'index.html'
        },
        {
            name: 'Features',
            link: 'features.html'
        },
        {
            name: 'About',
            link: 'about.html'
        },
        {
            name: 'Contact',
            link: '#',
            children: [
            {
                name: 'Twitter',
                link: 'https://twitter.com/w_zce'
            },
            {
                name: 'About',
                link: 'https://weibo.com/zceme'
            },
            {
                name: 'divider'
            },
            {
                name: 'About',
                link: 'https://github.com/zce'
            }
            ]
        }
    ],
    pkg: require('./package.json'),
    date: new Date()
}

module.exports = grunt => {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        clean: ['dist'],
        // 编译scss文件
        sass: {
            options: {
                sourceMap: true,
                implementation: sass
            },
            main: {
                files: [{
                    expand: true,
                    cwd: 'src/', 
                    src: ['**/*.scss'],
                    dest: 'dist/',
                    ext: '.css',
                    extDot: 'first'  
                }]
            }
        },
        //压缩CSS
        cssmin: {
            prod: {
                options: {
                    report: 'min'
                },
                files: [
                    {
                        expand: true,
                        cwd: 'dist/',
                        src: ['**/*.css'],
                        dest: 'dist/',
                        ext: '.min.css'
                    }
                ]
            }
        },
        //压缩图片和文字
        imagemin: {
            prod: {
                options: {
                    optimizationLevel: 7,
                    pngquant: true
                },
                files: [
                    {
                        expand: true,
                        cwd: 'src/',
                        src: ['assets/fonts/*', 'assets/images/*'],
                        dest: 'dist/'
                    }
                ]
            }
        },
        // 编译html模板
        web_swig: {
            options: {
                swigOptions:{
                    cache: false
                },
                getData: function(tpl){
                    return pkg
                }
            },
            your_target: {
                expand: true,
                cwd: 'src/',
                src: '**/*.html',
                dest: 'dist/'
            },
        },
        // 压缩html
        htmlmin: {
            options: {
                removeComments: true,
                collapseWhitespace: true,
                removeEmptyAttributes: true,
                removeCommentsFromCDATA: true,
                removeRedundantAttributes: true
            },
            build: {
                files: [{
                    expand: true,
                    cwd: 'dist/',
                    src: '**/*.html',
                    dest: 'dist/'
                }]
            }
        },
        // 编译js文件
        babel: {
            options: {
                sourceMap: true,
                presets: ['@babel/preset-env']
            },
            main: {
                files: [{
                    expand: true,
                    cwd: 'src/', 
                    src: ['**/*.js'],
                    dest: 'dist/',
                    ext: '.js',
                    extDot: 'first'  
                }]
            }
        },
        // 压缩js文件
        uglify: {
            options: {
                banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'//添加banner
            },
            buildall: {
                files: [{
                    expand:true,
                    cwd:'dist/',
                    src:'**/*.js',
                    dest: 'dist/'
                }]
            },
        },
        connect: {
            server: {
                options: {
                    port: 3000,
                    hostname: '*',
                    open: true,
                    livereload: 35729,
                    keepalive: true,
                    base: ['dist/']
                }
            }
        },
        //没有实现
        watch: {
        }
    })

    loadGruntTasks(grunt)
    grunt.registerTask('build', ['clean', 'sass', 'cssmin', 'imagemin', 'web_swig', 'htmlmin', 'babel', 'uglify'])
    grunt.registerTask('develop', ['build', 'connect'])
}
